/**
 * Created by zhouqian on 2017/4/26.
 */
var DB =  require('../db/db');


/**获取对应banner的产品*/
exports.getGoodsList=function(req,res,msg){
    var limit = req.body.limit;
    var id = req.body.id;
    var plan = req.body.plan;
    if(plan==0){
        var sql="select g.*,g.shop_price as ProPrice from k_goods as g WHERE g.bannerfl_id ='" +id+ "' LIMIT " +limit+ ",6";
    }
    if(plan==1) {
        var sql = "select g.*,g.shop_price as ProPrice from k_goods as g WHERE g.cat_id ='" + id + "' LIMIT " + limit + ",6";
    }
    if(plan==2) {
        var sql = "select g.*,g.shop_price as ProPrice from k_goods as g WHERE g.id ='" + id + "' LIMIT " + limit + ",6";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
/**获取首页热销、潮品商品信息*/
exports.getHomePagePro=function(req,res,msg){
    var flag = req.body.flag;
    var sql="select *,shop_price as ProPrice from k_goods WHERE " +flag+ " = '1' LIMIT 0,6";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
/**获取喜好的商品信息*/
exports.getLoveGoods=function(req,res,msg){
    var sql="select *,shop_price as ProPrice from k_goods ORDER BY sort_order LIMIT 0,6";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
/**获取店铺信息*/
exports.getShopById = function(req,res,msg){
    var id = req.body.id;
    var sql="select * from k_shop where id = '"+ id +"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
/**获取店铺分类*/
exports.getCatagoryName=function(req,res,msg){
    var shop_id = req.body.shop_id;
    var sql="select * from k_catagory_shop where shop_id ='"+shop_id+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
/**获取店铺产品*/
exports.getShopProinfo=function(req,res,msg){
    var shop_id = req.body.shop_id;
    var limit = req.body.limit;
    var catagory_id = req.body.catagory_id;
    console.log(catagory_id);
    if(catagory_id ==""){
        var sql="select g.*,gs.shop_id,gs.price as ProPrice from k_goods_shop as gs LEFT JOIN k_goods as g on gs.goods_id=g.id where gs.shop_id = '"+shop_id+"' AND gs.is_discount = 0 LIMIT " +limit+ ",6";
    }else{
        var sql="select g.*,gs.shop_id,gs.price as ProPrice from k_goods_shop as gs LEFT JOIN k_goods as g on gs.goods_id=g.id where gs.shop_id = '"+shop_id+"' AND gs.catagory_id ='" + catagory_id + "' AND gs.is_discount = 0 LIMIT " +limit+ ",6";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

exports.getSearch=function(req,res,msg){
    var sql="select g.*,gs.shop_id,gs.price as ProPrice from k_goods_shop as gs LEFT JOIN k_goods as g on gs.goods_id=g.id WHERE shop_id ='ff8080815cf283ce015cfce5cae20009'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**获取店铺新品发布的商品*/
exports.getIsNewProinfo=function(req,res,msg){
    var shop_id = req.body.shop_id;
    var sql="SELECT g.*,gs.action_start_time,gs.action_end_time,gs.person_number,gs.person_number_gd,date_format(gs.online_time, '%Y/%m/%d') as online_time from k_goods as g LEFT JOIN k_goods_shop gs on gs.goods_id=g.id where gs.is_new='1' and gs.shop_id='"+ shop_id +"';";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**根据商品id获取商品详情信息*/
exports.getProinfoById=function(req,res,msg){
    var goods_id = req.body.goods_id;
    var sql="SELECT g.*,gp.point,gp.point_price,gs.price,attr.attr_name,attr.attr_value from k_goods as g LEFT JOIN k_goods_point as gp ON gp.goods_id = g.id LEFT JOIN k_goods_shop as gs ON gs.goods_id = g.id LEFT JOIN k_attribute attr on g.attr_id=attr.id where g.id='"+ goods_id +"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};